package com.wawei.seeds.me;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.wawei.seeds.R;
import com.wawei.seeds.model.HeroInfo;
import com.wawei.seeds.services.WalnutPortraitServiceHandler;
import com.wawei.seeds.utils.ImageLoader;
import com.wawei.seeds.utils.RetrofitNetworkService;
import com.wawei.seeds.utils.WalnutBaseNofityHandler;
import com.wawei.seeds.utils.WalnutNotifyHandler;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import retrofit.RestAdapter;

/**
 *
 * Created by t27891 on 15/7/2.
 */
public class MeHeroPortraitActivity extends ActionBarActivity {
    @InjectView(R.id.walnut_hero_head)ImageView heroHeadImage;
    @InjectView(R.id.item_hero_alias)TextView heroAlias;
    @InjectView(R.id.item_hero_gender)TextView heroGender;
    @InjectView(R.id.item_hero_title)TextView heroTitle;
    @InjectView(R.id.item_hero_magics)TextView heroMagics;
    @InjectView(R.id.walnut_me_back)View backView;

    private WalnutPortraitServiceHandler serviceHandler;
    private MeHeroPortraitNotifyHandler notifyHandler;
    private ImageLoader imageLoader;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_walnut_hero_portrait);
        ButterKnife.inject(this);

        serviceHandler = new WalnutPortraitServiceHandler();
        notifyHandler = new MeHeroPortraitNotifyHandler(this);
        imageLoader = new ImageLoader(this, R.drawable.walnut_logo);

        init();
    }

    private void init(){
        backView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });

        RestAdapter restAdapter = RetrofitNetworkService.makeAsyncRestAdapter();
        Bundle bundle = new Bundle();
        bundle.putInt(WalnutPortraitServiceHandler.PORTRAIT_WHO, WalnutPortraitServiceHandler.PORTRAIT_HERO);
        serviceHandler.retrieveData(restAdapter, bundle, notifyHandler, WalnutNotifyHandler.INIT_ACHIEVE_DATA);
    }

    @Override
    protected void onDestroy() {
        imageLoader = null;
        serviceHandler = null;
        notifyHandler = null;
        super.onDestroy();
    }

    private static class MeHeroPortraitNotifyHandler extends WalnutBaseNofityHandler<MeHeroPortraitActivity, HeroInfo> {
        public MeHeroPortraitNotifyHandler(MeHeroPortraitActivity meHeroPortraitActivity) {
            super(meHeroPortraitActivity);
        }

        @Override
        public void doInitData(MeHeroPortraitActivity meHeroPortraitActivity, List<HeroInfo> data) {
            if(null != data && data.size() > 0){
                HeroInfo heroInfo = data.get(0);
                get().heroAlias.setText(heroInfo.alias);
                get().heroGender.setText(heroInfo.gender);
                get().heroTitle.setText(heroInfo.title);
                get().heroMagics.setText(heroInfo.magics);
                get().imageLoader.loadImage(heroInfo.headPortrait, get().heroHeadImage, true, false);
            }
        }
    }
}
